<template>
  <div class="">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <ProFilter v-model="filter.pro_id" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <SupplierSelect v-model="filter.sup_id" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" prop="begin_order_time">
          <el-date-picker
            v-model="filter.begin_order_time"
            placeholder="开始时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" prop="end_order_time">
          <el-date-picker
            v-model="filter.end_order_time"
            placeholder="结束时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="getList">查 询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border 
      :summary-method="getSummaries"
      show-summary
      fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" width="260px" prop="sup_name" />
      <el-table-column label="项目名称" width="260px" prop="pro_name" />
      <el-table-column label="合同编号" width="190px">
        <template slot-scope="{row}">
          {{ 'LOPO'+row.id }}
        </template>
      </el-table-column>
      <el-table-column label="合同日期" align="center" width="140px" prop="pur_time" />
      <el-table-column label="付款方式" width="320px" prop="paytype" />
      <el-table-column label="合同金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.price, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="追加金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.additional, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="追减金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.reduction, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="已付款" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.pay, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="未付款" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(app.toFix(row.price, 2) - app.toFix(row.pay, 2) + row.additional - row.reduction, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="已开票金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.has_tax, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="未开票金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(app.toFix(row.price, 2) - app.toFix(row.has_tax, 2) + row.additional - row.reduction, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="结算" width="80px" align="center" :formatter="incomeState" />
      <el-table-column label="合同类型" width="110px" align="center" :formatter="typeState" />
      <el-table-column label="备注" min-width="260px" prop="remark" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                :disabled="row.income_state && row.income_state === 1 ? true : false"
                @click.native="handleDialog({...row,
                                             ...{dTitle: '财务结算', dType: 'edit', dField: dField}})"
              >
                结 算
              </el-dropdown-item>
              <!-- <el-dropdown-item
                @click.native="handleDialog({...{'pro_id': row.id}, ...row,
                                             ...{dRef: 'pays', dTitle: '付款信息', dType: 'edit', dField: dFieldPay}})"
              >
                付 款
              </el-dropdown-item> -->
              <el-dropdown-item
                @click.native="handleDialog({...{sn: 'LOPO' + row.sn, pur_id: row.id, sup_id: row.sup_id, price: row.price}, ...
                {dTitle: '采购订单合同', dType: '', dField: dFieldContract, dRef: 'contract'}})"
              >合 同</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Pays ref="pays" :get-data="getList" :info="dialog" />
    <Contract ref="contract" :get-data="getList" :info="dialog" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
  </div>
</template>

<script>
import { purs } from '@/api/purchase'
import Pays from './components/pays-dialog'
import Dialog from './components/expend-dialog'
import ProFilter from '@/components/ProFilter'
import Contract from '@/views/purchase/components/mp/contract-dialog'
import page from '@/utils/page'
import SupplierSelect from '@/myComponents/SupplierSelect'

export default {
  name: '',
  components: { ProFilter, Dialog, Pays, Contract, SupplierSelect },
  extends: page,
  props: {
    reqList: {
      default: purs,
      type: Function
    }
  },
  data() {
    return {
      price: {},
      parameter: { states: '1,2' },
      dField: ['id', 'remark', 'state'],
      dFieldContract: ['pur_id', 'sn', 'sup_id', 'id', 'price', 'agent', 'phone', 'entrust', 'price', 'addr',
        'sign_time', 'tax', 'delivery_time', 'project', 'person', 'place', 'paytype'],
      dFieldPay: ['pro_id', 'expend_state', 'id']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    incomeState(e) {
      let s = '未结算'
      if (e.state && e.state === 2) s = '已结算'
      return s
    },
    typeState(e) {
      let s = ''
      if (e.type) {
        if (e.type === 1) {
          s = '物料采购'
        } else if (e.type === 2) {
          s = '设备采购'
        } else if (e.type === 3) {
          s = '工程外包'
        } else if (e.type === 4) {
          s = '库存出库'
        }
      }
      return s
    },
    getListAfter(e) {
      this.price = e.summary
    },
    getSummaries() {
      const sums = []
      sums[0] = '合计'
      sums[6] = this.app.toFix(this.price.price, 2)
      sums[7] = this.app.toFix(this.price.additional, 2)
      sums[8] = this.app.toFix(this.price.reduction, 2)
      sums[9] = this.app.toFix(this.price.pay, 2)
      sums[10] = this.app.toFix(this.price.price - this.price.pay + this.price.additional - this.price.reduction, 2)
      sums[11] = this.app.toFix(this.price.has_tax, 2)
      sums[12] = this.app.toFix(this.price.price - this.price.has_tax + this.price.additional - this.price.reduction, 2)
      return sums
    }
  }
}
</script>
